<template>
  <div>
    <van-loading v-if="loading" size="0.5rem" />
    <van-icon
      :color="value ? '#ffa500' : `#777`"
      :name="value ? 'star' : 'star-o'"
      @click="onCollect"
      v-else
    />
  </div>
</template>

<script>
import { addCollect, deleteCollect } from "@/api/article";
export default {
  name: "CollectArticle",
  props: {
    value: {
      type: Boolean,
      required: true,
    },
    id: {
      type: [String, Number],
      required: true,
    },
  },
  methods: {
    async onCollect() {
      try {
        this.loading = true;
        // 已收藏则调用取消收藏接口，未收藏就调用收藏接口
        if (this.value) {
          await deleteCollect(this.id);
        } else {
          await addCollect(this.id);
        }
        // 把要改成的收藏状态传给父组件
        this.$emit("input", !this.value);
      } finally {
        this.loading = false;
      }
    },
  },

  data() {
    return {
      loading: false,
    };
  },
};
</script>

<style>
</style>